import time
import random
from datetime import datetime

from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By

"""
代玉铃
使用seleniu爬取
300条
核心数据
爬取大约耗时28分左右
"""
url = 'https://quote.eastmoney.com/center/gridlist.html#hs_a_board'  # 网页地址

f = open("hs_300_hexin.csv", mode="w", encoding="utf-8")
f.write(f"公司,ID,"
        f"收益,PE"
        f"每股净资产,市净率"
        f"总营收,同比1"
        f"净利润,同比2"
        f"毛利率,净利率"
        f"ROE,负债率\n")
# 配置无头信息--预防被封ip
from selenium.webdriver.chrome.options import Options
opt = Options()
opt.add_argument("--headless")
opt.add_argument("--disable-gpu")
web = Chrome(options=opt)

web.get(url)# 驱动
web.maximize_window()# 最大化窗口

start_time = datetime.now()
print(start_time)#打印开始时间

for i in range(1,18):
    print(f"第{i}页")
    # 定位到table中，预防元素被遮挡
    head = web.find_elements(by=By.XPATH, value='//*[@id="tab"]')[0]
    web.execute_script("arguments[0].scrollIntoView();", head)
    # 叉掉广告
    block = web.find_elements(by=By.XPATH, value='//*[@class="newstsclose"]/span')
    if len(block) > 0:
        block[0].click()
    # 得到该页面全部详情页链接入口
    a_list = web.find_elements(by=By.XPATH, value='//*[@class="mywidth"]/a')

    for a in a_list:
        # 叉掉广告
        block = web.find_elements(by=By.XPATH, value='//*[@class="newstsclose"]/span')
        if len(block) > 0:
            block[0].click()
        # 跳转子页面
        a.click()
        time.sleep(random.randint(3, 5))
        web.switch_to.window(web.window_handles[-1])# 跳转视角
        # 提取公司名称+ID
        name_list = web.find_elements(by=By.XPATH, value='//*[@class="quote_title_l"]/span')[:-1]
        name = name_list[0].text
        ID = name_list[1].text
        # 提取公司核心数据
        table = web.find_elements(by=By.XPATH, value='//*[@class="quotecore"]/table/tbody/tr/td')
        if len(table) > 0:
            Earnings_str = table[0].text  # 收益
            Earnings=Earnings_str.split(":")[1]

            PE_str = table[1].text
            PE=PE_str.split(":")[1]

            Book_Value_per_Share_str = table[2].text  # 每股净资产
            Book_Value_per_Share = Book_Value_per_Share_str.split(":")[1]

            Price_to_Book_Ratio_str = table[3].text  # 市净率
            Price_to_Book_Ratio=Price_to_Book_Ratio_str.split(":")[1]

            Total_Revenue_str = table[4].text  # 总营收
            Total_Revenue=Total_Revenue_str.split(":")[1]

            Year_over_Year_Growth1_str = table[5].text  # 同比
            Year_over_Year_Growth1=Year_over_Year_Growth1_str.split(":")[1]

            Net_Profit_str = table[6].text  # 净利润
            Net_Profit=Net_Profit_str.split(":")[1]

            Year_over_Year_Growth2_str = table[7].text  # 同比
            Year_over_Year_Growth2=Year_over_Year_Growth2_str.split(":")[1]

            Gross_Margin_str = table[8].text  # 毛利率
            Gross_Margin=Gross_Margin_str.split(":")[1]

            Net_Margin_str = table[9].text  # 净利率
            Net_Margin=Net_Margin_str.split(":")[1]

            ROE_str = table[10].text
            ROE=ROE_str.split(":")[1]

            Debt_to_Equity_Ratio_str= table[11].text  # 负债率
            Debt_to_Equity_Ratio=Debt_to_Equity_Ratio_str.split(":")[1]

            print(f"{name},{ID},"
                  f"{Earnings},{PE},"
                  f"{Book_Value_per_Share},{Price_to_Book_Ratio},"
                  f"{Total_Revenue},{Year_over_Year_Growth1},"
                  f"{Net_Profit},{Year_over_Year_Growth2},"
                  f"{Gross_Margin},{Net_Margin},"
                  f"{ROE},{Debt_to_Equity_Ratio}")
            #存储数据
            f.write(f"{name},{ID},"
                    f"{Earnings},{PE},"
                    f"{Book_Value_per_Share},{Price_to_Book_Ratio},"
                    f"{Total_Revenue},{Year_over_Year_Growth1},"
                    f"{Net_Profit},{Year_over_Year_Growth2},"
                    f"{Gross_Margin},{Net_Margin},"
                    f"{ROE},{Debt_to_Equity_Ratio}\n")
        else:#有些公司没有核心数据板块
            f.write(f"{name},{ID}\n")
            print(f"{name},{ID}")
        web.close()# 关闭当前页面
        web.switch_to.window(web.window_handles[0])# 调整视角
    next=web.find_elements(by=By.XPATH,value='//*[@class="next paginate_button"]')[0]
    next.click()
    time.sleep(random.randint(3, 5))
end_time = datetime.now()
print(end_time)#打印结束时间
print("提取完毕")
f.close()
web.close()
